class ForumController < ApplicationController

  def index
    @system = System.find(:first)
    if @system
      @forum_pages, @forums = paginate(:forums, :per_page => 10, :order => 'position ASC')
    else
      redirect_to :controller => 'install'    
    end
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :index }

  def list
    redirect_to :action => 'index'
  end

  def up
    @forum = Forum.find(params[:id])
    @forum.move_higher
    redirect_to :action => 'index'
  end

  def down
    @forum = Forum.find(params[:id])
    @forum.move_lower
    redirect_to :action => 'index'
  end

  def show
    @forum = Forum.find(params[:id])
    @topic_pages, @topics = paginate(:topics, :conditions => ["forum_id = ? ", @forum.id], :per_page => 10, :order => "created_at ASC")
  end

  def new
    @forum = Forum.new
  end

  def create
    @forum = Forum.new(params[:forum])
    if @forum.save
      flash[:notice] = 'Forum was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @forum = Forum.find(params[:id])
  end

  def update
    @forum = Forum.find(params[:id])
    if @forum.update_attributes(params[:forum])
      flash[:notice] = 'Forum was successfully updated.'
      redirect_to :action => 'list'
    else
      render :action => 'edit'
    end
  end

  def delete
    Forum.find(params[:id]).delete
    flash[:notice] = 'Topic was successfully deleted.'
    redirect_to :action => 'list'
  end
end
